package org.oos.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import lombok.extern.slf4j.Slf4j;
import org.oos.config.OOSConfig;
import org.oos.service.OOSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;

@Service
@Slf4j
public class OOSServiceImpl implements OOSService {

    @Resource
    private OSS ossClient;

    @Autowired
    private OOSConfig oosConfig;

    @Override
    public void upload(MultipartFile uploadFile) throws IOException {
        //文件新路径
        try{
            String fileName = uploadFile.getOriginalFilename();
            ossClient.putObject(oosConfig.getBucketName(), fileName, new ByteArrayInputStream(uploadFile.getBytes()));
        } catch (OSSException oe) {
            System.out.println("捕获到一个OSSException，这意味着您向OSS发出了请求，但由于某种原因被拒绝并返回错误响应");
            System.out.println("错误消息:" + oe.getErrorMessage());
            System.out.println("错误代码:" + oe.getErrorCode());
            System.out.println("请求ID:" + oe.getRequestId());
            System.out.println("主机ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("捕获到ClientException，这意味着客户端在尝试与OSS通信时遇到严重的内部问题，例如无法访问网络。");
            System.out.println("错误消息:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }

    @Override
    public void delete(String objectName) {
        ossClient.deleteObject(oosConfig.getBucketName(), objectName);
    }

    @Override
    public void download(OutputStream os, String objectName) throws IOException {
        try {
            // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
            OSSObject ossObject = ossClient.getObject(oosConfig.getBucketName(), objectName);

            // 读取文件内容。
            BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
            BufferedOutputStream out = new BufferedOutputStream(os);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            out.flush();
            out.close();
            in.close();
        }catch (OSSException oe) {
            System.out.println("捕获到一个OSSException，这意味着您向OSS发出了请求，但由于某种原因被拒绝并返回错误响应");
            System.out.println("错误消息:" + oe.getErrorMessage());
            System.out.println("错误代码:" + oe.getErrorCode());
            System.out.println("请求ID:" + oe.getRequestId());
            System.out.println("主机ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("捕获到ClientException，这意味着客户端在尝试与OSS通信时遇到严重的内部问题，例如无法访问网络。");
            System.out.println("错误消息:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public List<OSSObjectSummary> list() {
        // 设置最大个数。
        final int maxKeys = 200;
        // 列举文件。
        ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(oosConfig.getBucketName()).withMaxKeys(maxKeys));

        return objectListing.getObjectSummaries();
    }
}
